<template>
  <div class="page-table">
    <YZTable
      v-bind="pageContentConfig"
      :dataList="dataList"
      :dataCount="dataCount"
      v-model:page="pageInfo"
    >
      <template #headerHandler>
        <el-button
          type="primary"
          size="medium"
          v-if="isCreate"
          @click="handleNewClick"
          v-text="pageContentConfig.newBtnText ?? '新建数据'"
        >
        </el-button>
      </template>

      <template #status="{ row }">
        <el-button
          size="mini"
          :type="row.enable ? 'success' : 'danger'"
          plain
          >{{ row.enable ? '启用' : '禁用' }}</el-button
        >
      </template>

      <template #createAt="scope">
        <strong>{{ $filters.formateTime(scope.row.updateAt) }}</strong>
      </template>

      <template #updateAt="scope">
        <strong>{{ $filters.formateTime(scope.row.updateAt) }}</strong>
      </template>

      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            type="text"
            size="mini"
            v-if="isUpdate"
            @click="handelEditClick(scope.row)"
          >
            <el-icon>
              <Edit></Edit>
            </el-icon>
            编辑
          </el-button>
          <el-popconfirm
            title="您确定要删除这条数据吗"
            @confirm="handleDeleteClick(scope.row)"
          >
            <template #reference>
              <el-button type="text" size="mini" v-if="isDelete">
                <el-icon>
                  <Delete></Delete>
                </el-icon>
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </div>
      </template>

      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <!-- 在page-table中动态插入剩余的插槽  -->
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </YZTable>
  </div>
</template>

<script lang="ts">
import { useStore } from '@/store'
// import eventBus from '@/utils/eventBus'
import { defineComponent, computed, ref, watch } from 'vue'
import { usePermission } from 'hooks/UsePermission'
import YZTable from '@/baseUi/table/src/YZTable.vue'
export default defineComponent({
  components: { YZTable },
  props: {
    pageContentConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const store = useStore()

    // 0.获取权限操作
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')

    //1. 双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 5 })

    watch(pageInfo, () => {
      getPageData()
    })

    //2. 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          ...queryInfo,
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize
        }
      })
    }
    getPageData()
    // eventBus.on('getPageData', getPageData)
    //3. 从vuex中获取数据
    const dataList = computed(() =>
      store.getters['system/pageListData'](props.pageName)
    )
    const dataCount = computed(() =>
      store.getters['system/pageListCount'](props.pageName)
    )

    //4.获取其他的动态插槽
    const otherPropSlots = props.pageContentConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )
    //5. 删除/编辑/新建操作
    const handleDeleteClick = (item: any) => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    const handleNewClick = () => {
      emit('newBtnClick')
    }
    const handelEditClick = (item: any) => {
      emit('editBtnClick', item)
    }
    return {
      dataList,
      dataCount,
      getPageData,
      pageInfo,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      isQuery,
      handleDeleteClick,
      handleNewClick,
      handelEditClick
    }
  }
})
</script>

<style lang="scss" scoped>
.page-table {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
